package com.aaa.controller;

import com.aaa.entity.Fuser;
import com.aaa.entity.ResultMsg;
import com.aaa.service.FuserServiceDao;
import com.alibaba.fastjson.JSON;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping("fuser")
@RestController
public class FuserController {
    @Resource
    FuserServiceDao fuserServiceDao;
    @RequestMapping("add")
    public ResultMsg add(Fuser users){
        System.out.println(JSON.toJSONString(users));
        System.out.println(users.getPassword());
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        users.setPassword(passwordEncoder.encode(users.getPassword()));
        return ResultMsg.success(fuserServiceDao.saveOrUpdate(users));
    }

    @RequestMapping("select")
    public List<Map>select(String uid){
        return fuserServiceDao.select(uid);
    }
    @RequestMapping("update")
    public boolean update(Fuser fuser){
        return fuserServiceDao.saveOrUpdate(fuser);
    }

    @RequestMapping("password")
    public boolean password(String a,String b){
        System.out.println(111);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(a,b);
        return matches;
    }
    @RequestMapping("updatePassword")
    public boolean pass(String password,int id){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(password);
        return fuserServiceDao.updatePassword(encode,id);
    }
}
